﻿using Microsoft.AspNetCore.Mvc;

//namespace FFile=System.IO;
//using System.IO as MyIO;
using IOFile = System.IO.File;



namespace FastCode.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class T4Controller : Controller
    {

        /// <summary>
        /// 测试
        /// </summary>
        /// <param name="entityName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Go(string entityName)
        {
            var basePath = AppContext.BaseDirectory + "/T4Template";

            #region service
            string serviceContent = await IOFile.ReadAllTextAsync(basePath + "/Services.txt");
            serviceContent = serviceContent.Replace("<%EntityName%>", entityName);
            var outPath = AppContext.BaseDirectory + "out";
            if (!Directory.Exists(outPath))
            {
                Directory.CreateDirectory(outPath);
            }
            await IOFile.WriteAllTextAsync($"{outPath}/{entityName}Service.cs", serviceContent);
            #endregion

            #region controller
            string controllerContent = await IOFile.ReadAllTextAsync(basePath + "/Controller.txt");
            controllerContent = controllerContent.Replace("<%EntityName%>", entityName);

            string firstLetterLoweredString = char.ToLower(entityName[0]) + entityName[1..];
            controllerContent = controllerContent.Replace("<%EntityNameParam%>", firstLetterLoweredString);
            await IOFile.WriteAllTextAsync($"{outPath}/{entityName}Controller.cs", controllerContent);
            #endregion


            //<%EntityName%>
            return Content($"生成成功:{outPath}");
        }
    }
}
